﻿///***************************************************************
/// 시  스  템 : Updms.Mny
/// 단위시스템 : MNY
/// 계      층 : Presentation Layer
/// 프로그램명 : Com2010r
/// 파  일  명 : Com2010r.aspx.cs
/// 설      명 : 사용자별 접속로그
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2012-10-02 송시명   최초 작성
///***************************************************************
using System;
using System.Data;
using System.Collections;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;

using SMFramework.Adapter;
using SMFramework.Data;
using SMFramework.Security;
using SMFramework.Utility;

using Base.Dac;
using Updms.Common.BaseClass;
using Updms.Com.Exe;


namespace Updms.Com
{
	public partial class Com2010r : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		//프로그램상수
		public static readonly string PID = "Com2010r";
		public static readonly string gs_path_info = "/updms/Com/" + PID + ".aspx";

		Global global = new Global();
		Crypto crypto = new Crypto();
		CommCode.Com com = new CommCode.Com(LANG_SET);

		//검색
		protected string kw1 = "";	//검색시작일
		protected string kw2 = "";	//검색종료일
		protected string kw3 = "";	//아이디
		protected string kw4 = "";	//정렬순서

		//메뉴 선택값
		protected string gs_sm = "";

		// 페이지모드
		private string gs_page_mode = "";

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
			}
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetClientScript();
			SetOrderBy();
			SetSearchParameter();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion

		#region SetClientScript

		private void SetClientScript()
		{
			//엔터검색
			txt_search_reg_dt_from.Attributes.Add("onkeypress", "enterSearch(event)");
			txt_search_reg_dt_to.Attributes.Add("onkeypress", "enterSearch(event)");
			txt_search_nm.Attributes.Add("onkeypress", "enterSearchUserInfo(event)");
		}

		#endregion

		#region SetInit, SetSearchParameter

		//초기값설정
		private void SetInit()
		{
			lbl_search_flag.Text = "INIT";

			gs_sm = (Request.QueryString["sm"] == null || Request.QueryString["sm"].Trim().Length != 7)
					? global.GetDirDepth(PID) : Request.QueryString["sm"].ToString();

			gs_page_mode = (Request.QueryString["page_mode"] == null || Request.QueryString["page_mode"].Trim().Length == 0)
					? "" : Request.QueryString["page_mode"].ToString();
		}

		//검색파라미터
		private void SetSearchParameter()
		{
			switch (lbl_search_flag.Text.Trim().ToUpper())
			{
				case "INIT":
					if (String.IsNullOrEmpty(Request.QueryString["kw1"]))
					{
						if (String.IsNullOrEmpty(txt_search_reg_dt_from.Text))
						{
							kw1 = DateUtil.GetMonthEx(-1, "yyyy-MM") + "-01";	//오늘부터 1개월전
							txt_search_reg_dt_from.Text = kw1;
						}
						else
						{
							kw1 = txt_search_reg_dt_from.Text.Trim();
						}
					}
					else
					{
						kw1 = Server.UrlDecode(Request.QueryString["kw1"]);
						txt_search_reg_dt_from.Text = kw1;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw2"]))
					{
						if (String.IsNullOrEmpty(txt_search_reg_dt_to.Text))
						{
							kw2 = DateUtil.GetMonthEx(0);	//오늘
							txt_search_reg_dt_to.Text = kw2;
						}
						else
						{
							kw2 = txt_search_reg_dt_to.Text.Trim();
						}
					}
					else
					{
						kw2 = Server.UrlDecode(Request.QueryString["kw2"]);
						txt_search_reg_dt_to.Text = kw2;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw3"]))
					{
						kw3 = txt_search_user_id.Text.Trim();
					}
					else
					{
						kw3 = Server.UrlDecode(Request.QueryString["kw3"]);
						txt_search_user_id.Text = kw3;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw4"]))
					{
						kw4 = ddl_search_order_by.SelectedValue;
					}
					else
					{
						kw4 = Server.UrlDecode(Request.QueryString["kw4"]);
						ddl_search_order_by.SelectedValue = kw4;
					}
					break;
				case "SEARCH":
					kw1 = String.IsNullOrEmpty(txt_search_reg_dt_from.Text) ? DateUtil.GetMonthEx(-1, "yyyy-MM") + "-01" : txt_search_reg_dt_from.Text.Trim();
					kw2 = String.IsNullOrEmpty(txt_search_reg_dt_to.Text) ? DateUtil.GetMonthEx(0) : txt_search_reg_dt_to.Text.Trim();
					kw3 = String.IsNullOrEmpty(txt_search_nm.Text.Trim()) ? "" : txt_search_user_id.Text.Trim();
					kw4 = ddl_search_order_by.SelectedValue;
					break;
			}
		}


		#endregion

		#region 마스터 Method (SetOrderBy)

		//정렬순서
		private void SetOrderBy()
		{
			com.SetOrderBy(ddl_search_order_by);
		}

		#endregion		

		#region 디테일 Method (SetAccessLogInfo, DrawAccessLogInfo)

		//접속현황
		private void SetAccessLogInfo(DataTable lo_dt)
		{
			GridView1.DataSource = lo_dt;
			GridView1.DataBind();
		}

		//예산대비지출금액챠트
		private void DrawAccessLogInfo(DataTable lo_dt)
		{
			//초기화
			chart_access_count.Data.Clear();

			//시리즈 갯수
			chart_access_count.Data.Series = 1;

			int li_count = lo_dt.Rows.Count;

			if (li_count > 0)
			{
				//Chart높이 (1행 = 22px)
				chart_access_count.Height = 22 * li_count + 28;

				for (int i = 0; i < li_count; i++)
				{
					//데이터바인딩
					chart_access_count.Data[0, i] = DataTableUtil.GetFldInt(lo_dt, (li_count - i) - 1, "Cnt");

					//X축 라벨링
					chart_access_count.AxisX.Labels[i] = DataTableUtil.GetFldStr(lo_dt, (li_count - i) - 1, "Pgm_Nm_Label");
				}
			}
		}

		#endregion

		#region CRUD (SearchProcess)

		//검색
		private void SearchProcess()
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			lbl_search_flag.Text = "SEARCH";
			SetSearchParameter();

			//검색
			lo_dp.AddProperty("ls_reg_dt_from", typeof(string), kw1);
			lo_dp.AddProperty("ls_reg_dt_to", typeof(string), kw2);
			lo_dp.AddProperty("ls_user_id", typeof(string), kw3);
			lo_dp.AddProperty("ls_view_order", typeof(string), kw4);
			lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);

			using (Com1011m_NT _Com1011m_NT = new Com1011m_NT())
			{
				lo_ds.Merge(_Com1011m_NT.GetAccessLog(lo_dp));
			}

			SetAccessLogInfo(lo_ds.Tables[0]);
			DrawAccessLogInfo(lo_ds.Tables[0]);
		}

		#endregion

		#region Page_PreRender, ChangePageMode

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				using (AccessCheck access = new AccessCheck())
				{
					//접근권한 체크
					if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
					{
						Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
					}
				}
			}
			ChangePageMode();
		}

		//ChangePageMode
		private void ChangePageMode()
		{
			switch (gs_page_mode)
			{
				case "":		//처음 페이지로드시
					table_list_set.Visible = false;
					break;
				case "R":		//조회시
					table_list_set.Visible = true;
					break;
			}
			SetGridViewHeader(global.GetColumnListByPgmId(PID, LANG_SET));
		}

		#endregion

		#region GridView이벤트

		protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			GridView gv = (GridView)sender;
			
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				//마우스올렸을 때
				e.Row.Attributes.Add("onMouseOver", GRID_ONMOUSEOVER_COLOR);
				e.Row.Attributes.Add("onMouseOut", GRID_ONMOUSEOUT_COLOR);
			}
		}

		#endregion

		#region 버튼이벤트

		//검색
		protected void ibtn_search_Click(object sender, EventArgs e)
		{
			gs_page_mode = "R";
			SearchProcess();
		}

		//엑셀
		protected void ibtn_excel_Click(object sender, EventArgs e)
		{
			SetSearchParameter();

			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			//검색
			lo_dp.AddProperty("ls_reg_dt_from", typeof(string), kw1);
			lo_dp.AddProperty("ls_reg_dt_to", typeof(string), kw2);
			lo_dp.AddProperty("ls_user_id", typeof(string), kw3);
			lo_dp.AddProperty("ls_view_order", typeof(string), kw4);
			lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);

			using (Com1011m_NT _Com1011m_NT = new Com1011m_NT())
			{
				lo_ds.Merge(_Com1011m_NT.GetAccessLog(lo_dp));
			}
			string fileName = PID + "(" + global.GetMenuTitle(PID, LANG_SET) + ")";
			ExcelUtil.CreateExcelFileX(this.Page, fileName, lo_ds, SetExcelHeaderX(global.GetColumnListByPgmId(PID, LANG_SET)));
		}

		#endregion

	}
}
